package com.corechina.apex_osswords.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.corechina.apex_osswords.common.Response;
import com.corechina.apex_osswords.common.ShiroUtils;
import com.corechina.apex_osswords.common.WeiXinUserUtils;
import com.corechina.apex_osswords.domain.dao.CoreEnterpriseEngineerMapper;
import com.corechina.apex_osswords.domain.dao.CoreMaintenanceEngineerMapper;
import com.corechina.apex_osswords.domain.dao.CoreProjectUserMapper;
import com.corechina.apex_osswords.domain.dao.CoreServiceEngineerMapper;
import com.corechina.apex_osswords.domain.dto.CoreEnterpriseEngineerDto;
import com.corechina.apex_osswords.domain.dto.CoreMaintenanceEngineerDto;
import com.corechina.apex_osswords.domain.entity.CoreEnterpriseEngineer;
import com.corechina.apex_osswords.domain.entity.CoreMaintenanceEngineer;
import com.corechina.apex_osswords.domain.entity.CoreProjectUser;
import com.corechina.apex_osswords.domain.entity.CoreServiceEngineer;
import com.corechina.apex_osswords.domain.vo.CoreMaintenanceEngineerVo;
import com.corechina.apex_osswords.service.ICoreMaintenanceEngineerService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 运维人员信息表 服务实现类
 * </p>
 *
 * @author cc
 * @since 2022-03-11
 */
@Service
public class CoreMaintenanceEngineerServiceImpl extends ServiceImpl<CoreMaintenanceEngineerMapper, CoreMaintenanceEngineer> implements ICoreMaintenanceEngineerService {

    @Autowired
    private CoreMaintenanceEngineerMapper coreMaintenanceEngineerMapper;

    @Autowired
    private CoreEnterpriseEngineerMapper coreEnterpriseEngineerMapper;

    @Autowired
    private CoreServiceEngineerMapper coreServiceEngineerMapper;

    @Autowired
    private CoreProjectUserMapper coreProjectUserMapper;

    @Override
    public Response add(CoreMaintenanceEngineerDto coreMaintenanceEngineerDto) {
        CoreMaintenanceEngineer coreMaintenanceEngineer = new CoreMaintenanceEngineer();
        BeanUtils.copyProperties(coreMaintenanceEngineerDto, coreMaintenanceEngineer);
        coreMaintenanceEngineer.setCreateEnterprise(coreMaintenanceEngineerDto.getEnterpriseId());
        if (coreMaintenanceEngineerMapper.selectCount(new QueryWrapper<CoreMaintenanceEngineer>().eq("tel", coreMaintenanceEngineer.getTel())) > 0) {
            return Response.error("该手机号人员已添加");
        }
        coreMaintenanceEngineerMapper.insert(coreMaintenanceEngineer);
        CoreEnterpriseEngineerDto coreEnterpriseEngineerDto = new CoreEnterpriseEngineerDto();
        coreEnterpriseEngineerDto.setEngineerId(coreMaintenanceEngineer.getId());
        coreEnterpriseEngineerDto.setEnterpriseId(coreMaintenanceEngineerDto.getEnterpriseId());
        coreEnterpriseEngineerDto.setServiceId(coreMaintenanceEngineerDto.getServiceId());
        this.addEnterpriseEngineer(coreEnterpriseEngineerDto);
        if (coreMaintenanceEngineerDto.getProjectId() != null) {
            this.addProjectUser(coreMaintenanceEngineer.getId(), coreMaintenanceEngineerDto.getProjectId());
        }
        return Response.ok();
    }

    private void addProjectUser(Integer engineerId, Integer projectId) {
        CoreProjectUser coreProjectUser = new CoreProjectUser();
        coreProjectUser.setProjectId(projectId);
        coreProjectUser.setUserId(engineerId);
        coreProjectUserMapper.insert(coreProjectUser);
    }

    @Override
    public Response getList(String name, String company, String engineerType, Integer serviceId, Integer userType, Integer pageIndex, Integer pageSize) {
        //userType 0  pc 用户 1 小程序用户
        Integer enterpriseId;
        if (userType.equals(0)) {
            enterpriseId = ShiroUtils.getSeeAll().equals(1) ? null : ShiroUtils.getEnterpriseId();
        } else {
            enterpriseId = WeiXinUserUtils.getEnterpriseId();
        }
        IPage<CoreMaintenanceEngineerVo> coreMaintenanceEngineerVoIPage = coreMaintenanceEngineerMapper.getList(new Page(pageIndex, pageSize), name, company, engineerType, serviceId, enterpriseId);
        return Response.ok(coreMaintenanceEngineerVoIPage.getRecords(), coreMaintenanceEngineerVoIPage.getTotal());
    }

    @Override
    public Response getOne(Integer id) {
        return Response.ok(coreMaintenanceEngineerMapper.getOne(id));
    }

    @Override
    public Response getByTel(String tel, Integer enterpriseId, Integer serviceId, Integer engineerType) {
        CoreMaintenanceEngineer coreMaintenanceEngineer = coreMaintenanceEngineerMapper.getByTel(tel);
        if (coreMaintenanceEngineer == null) {
            return Response.ok();
        }
        if (!coreMaintenanceEngineer.getEngineerType().equals(2) && engineerType.equals(2)) {
            return Response.error(2005, "该人员为运维人员");
        } else if (coreMaintenanceEngineer.getEngineerType().equals(2) && !engineerType.equals(2)) {
            return Response.error(2006, "该人员为联系人");
        }
        if (serviceId == null) {
            if (coreEnterpriseEngineerMapper.checkEngineer(enterpriseId, coreMaintenanceEngineer.getId()) > 0) {
                return Response.error(2001, "该人员已在企业库中");
            }
            return Response.error(2002, coreMaintenanceEngineer.getId());
        } else {
            if (coreServiceEngineerMapper.checkEngineer(serviceId, coreMaintenanceEngineer.getId()) > 0) {
                return Response.error(2003, "已在企业服务对象中");
            }
            return Response.error(2004, coreMaintenanceEngineer.getId());
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Response addEnterpriseEngineer(CoreEnterpriseEngineerDto coreEnterpriseEngineerDto) {
        if (coreEnterpriseEngineerMapper.checkEngineer(coreEnterpriseEngineerDto.getEnterpriseId(), coreEnterpriseEngineerDto.getEngineerId()) < 1) {
            CoreEnterpriseEngineer coreEnterpriseEngineer = new CoreEnterpriseEngineer();
            BeanUtils.copyProperties(coreEnterpriseEngineerDto, coreEnterpriseEngineer);
            coreEnterpriseEngineerMapper.insert(coreEnterpriseEngineer);
        }
        if (coreEnterpriseEngineerDto.getServiceId() != null) {
            if (coreServiceEngineerMapper.checkEngineer(coreEnterpriseEngineerDto.getServiceId(), coreEnterpriseEngineerDto.getEngineerId()) > 0) {
                return Response.ok();
            } else {
                CoreServiceEngineer coreServiceEngineer = new CoreServiceEngineer();
                coreServiceEngineer.setEngineerId(coreEnterpriseEngineerDto.getEngineerId());
                coreServiceEngineer.setServiceId(coreEnterpriseEngineerDto.getServiceId());
                coreServiceEngineerMapper.insert(coreServiceEngineer);
            }
        }
        if (coreEnterpriseEngineerDto.getProjectId() != null) {
            this.addProjectUser(coreEnterpriseEngineerDto.getEngineerId(), coreEnterpriseEngineerDto.getProjectId());
        }
        return Response.ok();
    }

    @Transactional
    @Override
    public Response delete(Integer id, Integer serviceId) {
        if (serviceId != null) {
            coreServiceEngineerMapper.deleteEngineer(serviceId, id);
            if (coreServiceEngineerMapper.checkCount(id, ShiroUtils.getEnterpriseId()) < 1) {
                coreEnterpriseEngineerMapper.delete(ShiroUtils.getEnterpriseId(), id);
            }
        } else {
            coreEnterpriseEngineerMapper.delete(ShiroUtils.getEnterpriseId(), id);
        }
        coreProjectUserMapper.deleteByProject(id, ShiroUtils.getEnterpriseId(), serviceId);
        return Response.ok();
    }
}
